package com.wuhan.template.module.home.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseApplication;
import com.wuhan.template.base.BaseViewHolder;
import com.wuhan.template.base.MyBaseAdapter;
import com.wuhan.template.module.home.entity.BaoLiaoEntity;

import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/11 0011.
 */
public class BaoLiaoAdapter extends MyBaseAdapter<BaoLiaoEntity> {

    private Context context;
    private ImageLoader loader;
    private DisplayImageOptions options;

    public BaoLiaoAdapter(Context context, List list) {
        super(context, list);
        this.context = context;
        loader = ((BaseApplication) (context.getApplicationContext())).getImageLoader();
        options = new DisplayImageOptions.Builder()
                .cacheOnDisk(true)
                .cacheInMemory(true)
                .build();
    }

    @Override
    public int getLayoutId() {
        return R.layout.baoliao_item;
    }

    @Override
    public BaseViewHolder getViewHolder() {
        return new ViewHolder();
    }

    @Override
    public void bindViews(View view, BaseViewHolder viewHolder) {
        ViewHolder holder = (ViewHolder) (viewHolder);
        BaoLiaoEntity baoLiao = getItem(holder.position);
        holder.userContent.setText(baoLiao.getTitle());
        holder.useName.setText(baoLiao.getUser_name());
        holder.address.setText(baoLiao.getName());

        String create_time = baoLiao.getCreate_time();
        holder.time.setText(create_time);
        // String userImageUrl = baoLiao.getHost() + baoLiao.getDir() + baoLiao.getMaterial_path() + baoLiao.getPic_name();
      if(baoLiao.getAvatar() instanceof List){
       List<Map<String,String>> avartars = (List<Map<String, String>>) baoLiao.getAvatar();
        if ( avartars != null&&avartars.size()>0) {
            String userImageUrl = avartars.get(0).get("host") +avartars.get(1).get("dir")+
                    avartars.get(2).get("filepath")+avartars.get(3).get("filename");
            loader.displayImage(userImageUrl,holder.userImage,options);
        }
      }


        List<Map<String, String>> pics = (List<Map<String, String>>) baoLiao.getPics();
        if (pics != null && baoLiao.getPics() instanceof List) {
            //每个item的预览图片待解决
            int size = pics.size();
           /* List<ImageView> imageViews = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                imageViews.add(new ImageView(context));
            }*/
            for (int i = 0; i < size; i++) {
            ImageView imageView = new ImageView(context);
                String url = pics.get(i).get("host") + pics.get(i).get("dir")
                        + pics.get(i).get("filepath") + pics.get(i).get("filename");
                //holder.layout.setHapticFeedbackEnabled(true);
                loader.displayImage(url, imageView, options);
                holder.layout.addView(imageView);
            }
        }
    }
        public static class ViewHolder extends BaseViewHolder {
            @BindView(R.id.user_touxiang)
            ImageView userImage;
            @BindView(R.id.user_name)
            TextView useName;
            @BindView(R.id.baoliao_context)
            TextView userContent;
            @BindView(R.id.baoliao_address)
            TextView address;
            @BindView(R.id.baoliao_date)
            TextView time;
            @BindView(R.id.imageLayout)
            LinearLayout layout;
        }

    }

